package com.jiepos.mpos.biz.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jiepos.mpos.biz.service.service.InsServiceParamService;
import com.jiepos.mpos.biz.service.service.ServiceInfService;
import com.jiepos.mpos.biz.service.service.ServiceParamService;
import com.jiepos.mpos.biz.user.service.AppInsInfService;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.App_Ins_InfEntity;
import com.jiepos.mpos.common.entity.App_Service_InfEntity;
import com.jiepos.mpos.common.entity.App_Service_ParamEntity;
import com.jiepos.mpos.common.entity.InsParamSaveEntity;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 机构业务参数维护Controller
 * @author Zhao.Jinkui
 * @version 1.0
 * @ProjectName: mpos
 * @Package com.jiepos.mpos.biz.service.controller
 * @ClassName InsServiceParamController
 * @history
 * @UpdateRemark []
 * @since 2017/9/29
 */
@RequestMapping("insServiceParam")
@Controller
public class InsServiceParamController extends BaseController{

    @Autowired
    private InsServiceParamService insServiceParamService;

    @Autowired
    private ServiceParamService serviceParamService;

    @Autowired
    private AppInsInfService appInsInfService;

    @Autowired
    private ServiceInfService serviceInfService;

    /**
     * 进入机构业务参数页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getInsServiceParamList.do")
    public ModelAndView getInsServiceParamList(HttpServletRequest request, HttpServletResponse response){
        ModelAndView model = new ModelAndView("/module/service/insserviceparam/insserviceparam_list");
        return super.getDetail(request, response, model);
    }

    /**
     * 查询机构业务参数Json数据
     * @param pagination
     * @param appServiceInfEntity
     * @param appInsInfEntity
     * @return
     */
    @RequestMapping("/getInsServiceParamListJson.do")
    public @ResponseBody
    Map getInsServiceParamListJson(HttpServletRequest request,Pagination pagination, App_Service_InfEntity appServiceInfEntity, App_Ins_InfEntity appInsInfEntity){
        return insServiceParamService.findByCondition(request,pagination,appServiceInfEntity,appInsInfEntity);
    }

    /**
     * 进入新增机构业务参数界面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getInsServiceParamAdd.do")
    public ModelAndView getServiceParamAdd(HttpServletRequest request, HttpServletResponse response){
        ModelAndView model = new ModelAndView("/module/service/insserviceparam/insserviceparam_detail");
        return super.getDetail(request, response, model);
    }

    /**
     * 获取业务树(编辑)
     * @param ins_uuid 机构uuid
     * @return
     */
    @RequestMapping("/getServiceInfTreeEdit.do")
    @ResponseBody
    public List<Map<String, Object>> getServiceInfTreeEdit(String ins_uuid){
        return insServiceParamService.getServiceInfTreeEdit(ins_uuid);
    }


    /**
     * 保存业务参数及限额信息
     *
     * @param request
     * @param response
     * @param insParamSaveEntity
     * @return
     */
    @RequestMapping("/saveInsServiceParam.do")
    @ResponseBody
    public Map<String,String> saveInsServiceParam(HttpServletRequest request, HttpServletResponse response, InsParamSaveEntity insParamSaveEntity) {
        Map<String,String> result = new HashMap<>();
        if (!StringUtils.isBlank(insParamSaveEntity.getIns_uuid()) && !StringUtils.isBlank(insParamSaveEntity.getService_uuid())
                && !StringUtils.isBlank(insParamSaveEntity.getService_param_uuid())) {
            //获取该系统业务参数信息
            App_Service_ParamEntity serviceParam = serviceParamService.findByUUID(insParamSaveEntity.getService_param_uuid());
            //进行校验（机构扣率等相关值不能低于系统业务参数设置的值）
            List<String> list1 = insServiceParamService.checkInsParam(serviceParam, insParamSaveEntity.getAppInsServiceParamEntity1());
            List<String> list2 = insServiceParamService.checkInsParam(serviceParam, insParamSaveEntity.getAppInsServiceParamEntity2());
            if(list1.size() > 0 || list2.size() >0){
                //不能保存
                StringBuffer sb = new StringBuffer();
                if(list1.size() > 0){
                    sb.append("实名认证:");
                    sb.append(list1.toString());
                }
                if(list2.size() >0){
                    sb.append("VIP认证:");
                    sb.append(list2.toString());
                }
                sb.append("低于系统设定值，请修改");
                result.put("code",SystemConstant.ERROR);
                result.put("message",sb.toString());
            }else{
                //可以保存
                String saveResult = insServiceParamService.saveInsServiceParam(super.getLoginUser(request, response),
                        insParamSaveEntity.getAppInsServiceParamEntity1(), insParamSaveEntity.getAppInsServiceParamEntity2(),
                        insParamSaveEntity.getAppInsParamLimitEntity12(), insParamSaveEntity.getAppInsParamLimitEntity11(),
                        insParamSaveEntity.getAppInsParamLimitEntity22(), insParamSaveEntity.getAppInsParamLimitEntity21());
                if(SystemConstant.SUCCESS.equals(saveResult)){
                    result.put("code",SystemConstant.SUCCESS);
                    result.put("message","保存成功！");
                }else{
                    result.put("code",SystemConstant.ERROR);
                    result.put("message","保存失败!");
                }
            }

        } else {
            result.put("code",SystemConstant.ERROR);
            result.put("message","请点击左侧业务参数！");
        }
        return result;
    }

    /**
     * 加载业务参数设置数据
     * @param ins_uuid
     * @param service_uuid
     * @param service_param_uuid
     * @return
     */
    @RequestMapping("/getInsServiceParamByUUID.do")
    @ResponseBody
    public ModelAndView getInsServiceParamByUUID(String ins_uuid,String service_uuid,String service_param_uuid,String menuCode){
        ModelAndView model = null;
        String type = serviceInfService.getServiceTypeByServiceUUID(service_uuid);
        if(type != null && "5".equals(type)){//提现类业务
            model = new ModelAndView("/module/service/insserviceparam/insserviceparam_5_edit");
        }else if(type != null && "3".equals(type)){//非支付类业务
            model = new ModelAndView("/module/service/insserviceparam/insserviceparam_3_edit");
        }else{
            model = new ModelAndView("/module/service/insserviceparam/insserviceparam_edit");
        }
        model.addObject("menuCode",menuCode);
        if(!StringUtils.isBlank(ins_uuid) && !StringUtils.isBlank(service_uuid) && !StringUtils.isBlank(service_param_uuid)){
            Map<String,Object> map = insServiceParamService.getInsServiceParamDataByUUID(ins_uuid, service_uuid, service_param_uuid);
            for(Map.Entry<String, Object> entry : map.entrySet()){
                model.addObject(entry.getKey(),entry.getValue());
            }
            model.addObject("ins_uuid",ins_uuid);
            model.addObject("service_uuid",service_uuid);
            model.addObject("service_param_uuid",service_param_uuid);
        }
        return model;
    }

    /**
     * 更新机构参数设置树
     * @param request
     * @param response
     * @param ins_uuid
     * @param service_param_uuids
     * @return
     */
    @RequestMapping("/updateInsParamSetTree.do")
    @ResponseBody
    public String updateInsParamSetTree(HttpServletRequest request, HttpServletResponse response,String ins_uuid,String service_param_uuids){
        return insServiceParamService.updateInsParamSetTree(ins_uuid,service_param_uuids,super.getLoginUser(request, response));
    }

    /**
     * 进入修改机构业务参数页面
     * @param request
     * @param response
     * @param ins_uuid
     * @return
     */
    @RequestMapping("/getInsServiceParamDetail.do")
    @ResponseBody
    public ModelAndView getInsServiceParamDetail(HttpServletRequest request, HttpServletResponse response,String ins_uuid){
        ModelAndView model = new ModelAndView("/module/service/insserviceparam/insserviceparam_detail");
        //根据机构id加载机构信息 "INS_CD" "INS_CN_NM"
        if(!StringUtils.isBlank(ins_uuid)){
            String[] split = ins_uuid.split(":");
            ins_uuid = split[0];
        }
        Map appInsInfEntity=appInsInfService.getInsInfById(request,ins_uuid);
        model.addObject("ins_uuid",ins_uuid);
        model.addObject("inst_name",appInsInfEntity.get("INS_CD")+"-"+appInsInfEntity.get("INS_CN_NM"));
        return super.getDetail(request, response, model);
    }
}
